//
//  GSYNavigationController.swift
//  GSY-微博-swift
//
//  Created by Song on 2017/11/24.
//  Copyright © 2017年 Song. All rights reserved.
//

import UIKit

class GSYNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 隐藏默认的 NavigationBar
        navigationBar.isHidden = true;
    }

    // 重写 push 方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 如果不是栈底控制器，才需要隐藏，根控制器不需要处理
        if childViewControllers.count > 0 { // 大于0 说明栈里已经有了控制器（不需要隐藏的控制器）
            // 隐藏底部的 tabbar
            viewController.hidesBottomBarWhenPushed = true;
            
            // 判断控制器的类型
            if let vc = viewController as? GSYBaseViewController {
                
                // 判断控制器级数
                if childViewControllers.count == 1 {
                    vc.navItem.leftBarButtonItem = UIBarButtonItem(title: "首页", fontSize: 16, target: self, action: #selector(popToParent), isBack: true)
                } else {
                    // 取出自定义的 navItem
                    vc.navItem.leftBarButtonItem = UIBarButtonItem(title: "返回", fontSize: 16, target: self, action: #selector(popToParent), isBack: true);
                }
            }
        }
        
        
        
        super.pushViewController(viewController, animated: true)
    }
    
    @objc fileprivate func popToParent() {
        popViewController(animated: true)
    }

}
